<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header"><?php echo __('Product') ?></h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                
                <div class="panel-body">
                    <div class="row">
                        <?php
                            echo $this->Form->create('BoostCake', array(
                                'inputDefaults' => array(
                                    'div' => 'form-group',
                                    'wrapInput' => false,
                                    'class' => 'form-control',
                                    'method' => 'post'
                                ),
                                //'class' => 'well',
                                'enctype' => 'multipart/form-data'
                            ));
                            ?>
                        <div class="<?php echo $class_table ?>">
                            
                            <fieldset>
                                <legend>
                                    <?php echo __('Product') ?></legend>
                                <?php
                                echo $this->Form->input('Product.category_id', array(
                                    'label' => __('Select Category'),
                                    'type' => 'select',
                                    'options' => $categories,
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.product_type', array(
                                    'label' => false,
                                    'type' => 'hidden'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.image', array(
                                    'label' => __('File'),
                                    'type' => 'File',
                                    'src' => $this->webroot . '' . $base_config_url . '' . $name,
                                ));
                                echo '<img src="' . $this->webroot . $base_config_url . $name . '" style="width:100px;height:80px;">';
                                ?>
                                
                                
                                <div id="list_multiimg">
                                    <?php if(is_array($MultiImages)){foreach($MultiImages as $value_img){?>
                                    <div class="divimg_multi">
                                    <a class="condition_remove_img glyphicon glyphicon-remove"></a>
                                    <?php
                                        echo '<img title="'.$value_img['ProductImage']['url'].'" src="' . $this->webroot . $base_config_url . $value_img['ProductImage']['url'] . '" style="width:100px;height:80px;">';
                                        
                                    ?>
                                        <input type="hidden" value="<?php echo $value_img['ProductImage']['image_id']  ?>" name="ArrMultiImg[<?php echo $value_img['ProductImage']['image_id']  ?>]" />
                                    </div>    
                                    <?php }}?>
                                    <div class="clear"></div>
                                </div>
                                <?php $key = -1; ?>
                                
                                <div id="container_value" style="margin:10px 0px 10px 0px;">
                                    <div class="form-group" ></div>
                                </div>
                                <div class="btn" id="add_value"><a class="glyphicon glyphicon-plus"><?php echo __('Add'); ?></a></div>
                                
                                <?php
                                echo $this->Form->input('Product.price', array(
                                    'label' => __('Price'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.location_id', array(
                                    'label' => __('Select Location'),
                                    'type' => 'select',
                                    'options' => $location,
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Product.address', array(
                                    'label' => __('Address'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.name', array(
                                    'label' => __('Product Name'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.description', array(
                                    'label' => __('Product Description'),
                                    'row' => '10',
                                    'class' => 'ckeditor',
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.seo_title', array(
                                    'label' => __('SEO').' '.__('Title'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.seo_keyword', array(
                                    'label' => __('SEO').' '.__('Key word'),
                                    'type' => 'text'
                                ));
                                ?>
                                <?php
                                echo $this->Form->input('Productlanguage.seo_description', array(
                                    'label' => __('SEO').' '.__('Description'),
                                    'row' => '5'
                                ));
                                ?>
                                <?php
                                echo $this->Form->submit('Save', array(
                                    'div' => 'form-group',
                                    'class' => 'btn btn-default'
                                ));
                                ?>
                            </fieldset>
                        </div>
                        <div class="col-lg-6">

                        <?php
                        echo $this->Form->create('BoostCake', array(
                            'inputDefaults' => array(
                                'div' => 'form-group',
                                'wrapInput' => false,
                                'class' => 'form-control',
                                'method' => 'post'
                            ),
                            'class' => 'well',
                            'enctype' => 'multipart/form-data'
                        ));
                        ?>
                        <fieldset>
                            <?php foreach($properties as $value):?>
                                
                                <!-- select -->
                                <?php if($value['type'] == 1):?>
                                    <?php 
                                    echo $this->Form->input('ProductPropertyValue.'.$value['property_id'].'.property_value_id', 
                                        array(
                                            'label' => $value['name'],
                                            'type' => 'select',
                                            'options' => $value['values']
                                        ));
                                    ?>
                                <?php endif;?>
                            
                                <!-- multiple select -->
                                <?php if($value['type'] == 2):?>
                                    <?php 
                                    echo $this->Form->input('ProductPropertyValueArr.'.$value['property_id'].'.property_value', 
                                        array(
                                            'label' => $value['name'],
                                            'type' => 'select',
                                            'multiple' => true,
                                            'options' => $value['values']
                                        ));
                                    ?>
                                <?php endif;?>
                                
                                <!-- text box -->
                                <?php if($value['type'] == 3):?>
                                    <?php 
                                    echo $this->Form->input('ProductPropertyValueText.'.$value['property_id'].'.property_value_language', 
                                        array(
                                            'label' => $value['name'],
                                            'type' => 'text',
                                        ));
                                    ?>
                                <?php endif;?>
                                
                            <?php endforeach;?>
                        </fieldset>
                    </div>
                     	<?php echo $this->Form->end(); ?>
                    </div>

                </div>
                
                       
            </div>
            <!-- /.row (nested) -->
        </div>
        <!-- /.panel-body -->
    </div>
    <!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>


<?php echo $this->Html->script('ckeditor/ckeditor');  ?>

<script>
            
    $(document).ready(function(){
        var key = <?php echo $key;?>;
        var html = '<div class="group_value_property">\n\
                        <a class="condition_remove glyphicon glyphicon-remove"></a>\n\
                            <input type="file" name="data[multiImages][key][value]" class="form-control">\n\
                    </div>';
        $("#add_value").on('click',function(){
           key = key+1;
           var add = html.replace(new RegExp("key", 'g'),key);
           $("#container_value .form-group").last().after(add);
        });
        
        $(document).on('click','.condition_remove',function(){
            var div_parent = $(this).parents('.group_value_property');
            div_parent.remove();
	});
        
        $(document).on('click','.condition_remove_img',function(){
            var div_parent = $(this).parents('.divimg_multi');
            var input_hideen = div_parent.find( 'input[type=hidden]');
            if(input_hideen.size() == 1){
                
                console.log(input_hideen.val());
                $.ajax({
                    url: '<?php echo $this->Html->url('/product/delete')?>',
                    data: {'id':input_hideen.val()},
                    type: 'post',
                    dataType: 'json',
                    success: function(data){
                        if(data.success){
                            div_parent.remove();
                        }
                    }
                });
                
            }else{
                div_parent.remove();
            }
	});
        
    });
</script>